
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
* @author :${author}
* @description: ${desc}服务接口实现类
* @create date: ${createDate}
*/

@Slf4j
@Service
public class ${entityName}ServiceImpl implements ${entityName}Service {

    @Autowired
    ${entityName}Dao ${entityNameLower}Dao;


    /**
    * 保存${desc}
    * @param ${entityNameLower}Dto
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(${entityName}Dto ${entityNameLower}Dto) {
        return ${entityNameLower}Dao.insert(copyTo${entityName}(${entityNameLower}Dto));
    }

    /**
    * 根据ID更新${desc}
    * @param ${entityNameLower}Dto
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(${entityName}Dto ${entityNameLower}Dto) {
        return ${entityNameLower}Dao.update(copyTo${entityName}(${entityNameLower}Dto));
    }

<#if primaryKey!='noPrimaryKey'>
    /**
    * 删除${desc}
    * @param ${primaryKey}
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(${javaKeyType} ${primaryKey}) {
        return ${entityNameLower}Dao.deleteByPrimaryKey(${primaryKey});
    }

    /**
    * 根据查询${desc}
    * @param ${primaryKey}
    * @return
    */
    @Override
    public ${entityName}Vo get(${javaKeyType} ${primaryKey}) {
    ${entityName} ${entityNameLower}= ${entityNameLower}Dao.get(${primaryKey});
        return copyTo${entityName}Vo(${entityNameLower});
    }
</#if>

    /**
    * 查询${desc}
    * @param ${entityNameLower}Dto
    * @return
    */
    @Override
    public List<${entityName}Vo> findList(${entityName}Dto ${entityNameLower}Dto) {
        List<${entityName}> list= ${entityNameLower}Dao.findList(copyTo${entityName}( ${entityNameLower}Dto));
        return EntityUtils.copyTo(list,${entityName}Vo.class);
    }

    /**
    * 查询${desc}分页信息
    * @param ${entityNameLower}Dto
    * @return PageVO
    */
    @Override
    public PageVO findPageList(${entityName}Dto ${entityNameLower}Dto) {
        List<${entityName}Vo> list = findList(${entityNameLower}Dto);
        PageVO pageVO = new PageVO();
        if (!CollectionUtils.isEmpty(list)) {
        PageInfo<${entityName}Vo> ${entityNameLower}PageInfo = new PageInfo<>(list);
            pageVO.setList(list);
            pageVO.setTotalCount(${entityNameLower}PageInfo.getTotal());
        }
        return pageVO;
    }

    private ${entityName} copyTo${entityName}(${entityName}Dto ${entityNameLower}Dto) {
        ${entityName} ${entityNameLower} = new ${entityName}();
        BeanUtils.copyProperties(${entityNameLower}Dto, ${entityNameLower});
        return ${entityNameLower};
    }

    private ${entityName}Vo copyTo${entityName}Vo(${entityName} ${entityNameLower}) {
        ${entityName}Vo ${entityNameLower}Vo=new ${entityName}Vo();
        BeanUtils.copyProperties(${entityNameLower},${entityNameLower}Vo);
        return ${entityNameLower}Vo;
    }
}